package com.hy.study.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;

import java.util.List;
import java.util.Map;

/**
 * @author huyong
 * @date 2024 年 01 月 10 日 9:49: 44
 */
@Slf4j
public class YmlUtil {

    public static void sourceLoader() throws Exception {
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        List<PropertySource<?>> application = loader.load("application", new ClassPathResource("application.yml"));
        for (PropertySource propertySource : application) {
            String name = propertySource.getName();
            log.info("{}", name);
        }

    }

    public static void mapLoader() throws Exception {
        YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
        yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
        Map<String, Object> object = yamlMapFactoryBean.getObject();
        object.forEach((k, v) -> log.info("{} -->{}", k, v));

    }

    public static void main(String[] args) throws Exception {
        //sourceLoader();
        mapLoader();
    }
}
